local skel = fk.CreateSkill{
  name = "lb__chanyi",
}
local DIY = require "packages.diy_utility.diy_utility"

Fk:loadTranslationTable{
  ["lb__chanyi"] = "缠忆",
  --[":lb__chanyi"] = "你造成或受到伤害后，你可以观看上张牌使用者的手牌，令其弃置其中一张。若不为伤害牌，你获得本回合弃牌堆中花色相同的另一张牌，否则你结束本阶段。",
  --11.6
  [":lb__chanyi"] = "你造成或受到伤害后，你可以观看一名角色的手牌，令其弃置其中一张。若不为伤害牌，你从本回合弃牌堆中选择与之花色相同的另一张牌并获得之，否则你结束本阶段。",

  --["#lb__chanyi"] = "缠忆：你可以观看 %dest 的手牌，令其弃置其中一张",
  ["#lb__chanyi"] = "缠忆：你可以观看一名角色的手牌，令其弃置其中一张",
  ["#lb__chanyi_view"] = "缠忆：观看 %dest 的手牌",
  ["#lb__chanyi_dis"] = "缠忆：请弃置一张手牌",
  ["#lb__chanyi_choose"] = "缠忆：获得与 %arg 花色相同的另一张牌",

  ["@lb__chanyi"] = "缠忆",

  ["$lb__chanyi1"] = "记忆是常变的泡沫。",
  ["$lb__chanyi2"] = "既定的轨迹已然显现。",
}

local chanyi_spec = {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    --[[
    if target == player and player:hasSkill(skel.name) then
      local lastUseEvent = player.room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
        return true
      end, 0)[1]
      if lastUseEvent == nil then return false end
      local last_use = lastUseEvent.data
      if not last_use.from:isKongcheng() then
        event:setCostData(self, {from = last_use.from})
        return true
      end
    end
    --]]
    return target == player and player:hasSkill(skel.name) and table.find(player.room.alive_players, function (to)
      return not to:isKongcheng()
    end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local tos = table.filter(player.room.alive_players, function (to)
      return not to:isKongcheng()
    end)
    if #tos > 0 then
      tos = room:askToChoosePlayers(player, {
        targets = tos,
        min_num = 1,
        max_num = 1,
        skill_name = skel.name,
        cancelable = true,
        prompt = "#lb__chanyi"
      })
      if #tos > 0 then
        event:setCostData(self, {tos = tos})
        return true
      end
    end
    --return player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#lb__chanyi::"..event:getCostData(self).from.id})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    --local from = event:getCostData(self).from ---@type ServerPlayer
    local from = event:getCostData(self).tos[1]
    if from ~= player then
      room:viewCards(player, {cards = from:getCardIds("h"), skill_name = skel.name, prompt = "#lb__chanyi_view::"..from.id})
    end
    local ids = room:askToDiscard(from, {
      min_num = 1,
      max_num = 1,
      prompt = "#lb__chanyi_dis",
      cancelable = false,
      include_equip = false,
      skill_name = skel.name,
    })
    if #ids > 0 then
      local card = Fk:getCardById(ids[1])
      if card.is_damage_card then
        player:endCurrentPhase()
      else
        ids = table.filter(DIY.getCenterCards(room), function (id)
          return id ~= card.id and Fk:getCardById(id).suit == card.suit
        end)
        if #ids > 0 then
          if #ids > 1 then
            ids =  room:askToCards(player, {
              min_num = 1,
              max_num = 1,
              skill_name = skel.name,
              pattern = tostring(Exppattern {id = ids}),
              expand_pile = ids,
              prompt = "#lb__chanyi_choose:::"..card:toLogString(),
              cancelable = false,
            })
          end
          room:obtainCard(player, ids, true, fk.ReasonJustMove, player, skel.name)
        end
      end
    end
  end,
}

skel:addEffect(fk.Damage, chanyi_spec)
skel:addEffect(fk.Damaged, chanyi_spec)
--宝宝标记
--[[
skel:addEffect(fk.CardUsing, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@lb__chanyi", target)
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@lb__chanyi", 0)
end)
--]]

skel:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

return skel